#-*- coding: utf-8 -*-
'''
Created on Aug 24, 2012

@author: bbg
'''

from sqlalchemy import Integer, Column, String, MetaData, Table
from sqlalchemy.orm import mapper

class Shop(object):
    '''
    分店信息
    '''
    
    _Mapper_ = None
    _company_id_ = -1    

    def __init__(self):
        self.name = ''
        self.region_id = 0
        self.address = ''
        self.contact = 0
        self.phone = ''
        self.fax = ''
        self.email = ''
        self.note = ''
        self.state=0;
        self.group_id=0;
        
    
    @staticmethod
    def doMapping(company_id):
        '''
        按company_id分库，每次生成对象前都要执行一次，以便map到正确的数据库上去
        '''
        if Shop._company_id_ == company_id:
            return
                
        if Shop._Mapper_ is not None:
            Shop._Mapper_.dispose()
            
        metaData = MetaData()
        shopTable = Table('shop', metaData,
                           Column('id', Integer, primary_key = True),
                           Column('name', String),
                           Column('region_id', Integer),
                           Column('address', String),
                           Column('contact', String),
                           Column('phone', String),
                           Column('fax', String),
                           Column('email', String), 
                           Column('note', String), 
                           Column('state', Integer),
                           Column('group_id', Integer),                           
                           schema = "company_%s" % company_id)
                
        Shop._Mapper_ = mapper(Shop, shopTable)
        
